﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.WindowsAzure.Samples.Phone.Storage;
using System.IO;
using System.Text;

namespace CloudGalleryPhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        CameraCaptureTask ctask;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            ctask = new CameraCaptureTask();
            ctask.Completed += new EventHandler<PhotoResult>(ctask_Completed);
        }

        void ctask_Completed(object sender, PhotoResult e)
        {
            // Take photo and upload to cloud
            if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
            {
                var blobClient = CloudStorageContext.Current.Resolver.CreateCloudBlobClient();
                var container = blobClient.GetContainerReference("pics");

                container.CreateIfNotExist(
                    BlobContainerPublicAccessType.Container,
                    response =>
                    {
                        var blob = container.GetBlobReference("pic.jpg");

                        blob.UploadFromStream(
                            e.ChosenPhoto,
                            response2 =>
                            {
                            });
                    });
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ctask.Show();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // upload existing blob to cloud
            var blobClient = CloudStorageContext.Current.Resolver.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference("pics5");
            //container.Metadata.Add("samplekey", "samplevalue");

            container.CreateIfNotExist(
                BlobContainerPublicAccessType.Container,
                response =>
                {
                    var blob = container.GetBlobReference("pic6.jpg");
                    var sampleStream = new MemoryStream(Encoding.UTF8.GetBytes("holidaysnaps"));

                    blob.UploadFromStream(
                        sampleStream,
                        response2 =>
                        {
                        });
                }
                );
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
        }


    }
}